<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2022/1/28 9:53
 * Desc:门票监听
 */
class Controller_Provider_Ticket_Listener extends Stourweb_Controller
{
    private $order;
    private $provider_channel_order_model;
    private $ticket_api;

    public function before()
    {
        parent::before();
        try {
            $ordersn = Common::remove_xss(Arr::get($_GET, 'ordersn', ''));
            if (!$ordersn) {
                throw new Exception('订单号为空');
            } else {
                $order = Model_Member_Order::order_info($ordersn);
                if ($order) {
                    if($order['haschild']==1){
                        $order = DB::select()->from('member_order')->where('pid','=',$order['id'])->execute()->current();
                        $order['totalprice'] = DB::select()->from('member_order_compute')
                            ->where('order_id','=',$order['id'])
                            ->execute()->get('total_price');
                    }
                    $this->order = $order;
                } else {
                    throw new Exception('查询订单失败');
                }
            }
            $api_model_res = Model_Provider_Api_Ticket_Order::get_api_model_by_order($order);
            if(!$api_model_res['status']){
                throw new Exception($api_model_res['msg']);
            }
            $this->order['provider_product_id'] = $api_model_res['provider_product_id'];
            $this->provider_channel_order_model = $api_model_res['provider_channel_order_model'];
            $this->ticket_api = $api_model_res['ticket_api'];
        } catch (Exception $e) {
            exit(json_encode(array('status' => 0, 'msg' => $e->getMessage())));
        }
    }

    public function action_create()
    {
        $res = $this->provider_channel_order_model->create($this->order, $this->ticket_api);
        exit(json_encode($res));
    }

    public function action_pay()
    {
        $res = $this->provider_channel_order_model->pay($this->order, $this->ticket_api);
        exit(json_encode($res));
    }

    public function action_refund()
    {
        $res = $this->provider_channel_order_model->refund($this->order, $this->ticket_api);
        exit(json_encode($res));
    }

    public function action_cancel()
    {
        $res = $this->provider_channel_order_model->cancel($this->order, $this->ticket_api);
        exit(json_encode($res));
    }
}